/**
 * author:Administrator
 * date:2016/3/1
 * desc: 认证服务
 */

"use strict";

/** 授权表 userAuth [name, pwd, role, status] */
let Repo = require('../persist').UserAuth;

let service = {};
module.exports = service;

/** 角色定义 */
let ROLE_ENUM = {
  manager: 'manager'            // 0,
  , customer: 'customer'        //100,
  , vendor: 'vendor'            //200,
  , shopkeeper: 'shopkeeper'    //300
};
service.ROLE_ENUM = ROLE_ENUM;

/** 审核状态 */
let STATUS_ENUM = {
  wait: 'wait'            //1,
  , pass: 'pass'          //2,
  , reject: 'reject'      //3
};
service.STATUS_ENUM = STATUS_ENUM;

/**
 * 用户注册
 * @param name
 * @param pwd
 */
service.register = function *(name, pwd) {
  // find first
  let exist = yield Repo.findByName(name);
  if (exist) return null;
  let user = {
    name: name,
    pwd: pwd
  };
  yield Repo.store(user);
  return user;
};

service.destroyUser = function*(uId, isForce) {
  yield Repo.destroy(uId, isForce);
};


/**
 * 通用登录
 * @param name
 * @param pwd
 */
service.login = function *(name, pwd) {
  return yield Repo.findByNameAndPwd(name, pwd);
};